#include "LinkList.h"
#include "common.h"
#include "RandomList.h"
#include "string-util.h"


USING_NS_TEST;


elloop_test::LinkList::LinkList( Node* head /*= 0*/ ) : _head(head) {
	
}


void elloop_test::LinkList::removeDuplicate() {
	Node * next(_head);
	Node * cur(_head);
	while (cur && cur->_link) {
		next= cur->_link;
		if (cur->_data == next->_data) {
			cur->_link = next->_link;
			delete next;
		}
		else {
			cur = next;
		}
	}
}

bool elloop_test::LinkList::create() {
	RandomList * randomList = new RandomList();
	Node * tail = _head;
	for (int i=0; i<randomList->size(); ++i) {
		Node* node = new Node((*randomList)[i], 0);
		if (!_head) {
			_head = node;
			tail = _head;
		}
		else {
			tail->_link = node;
			tail = node;
		}
	}
	delete randomList;
	return true;
}

elloop_test::LinkList::~LinkList() {
	Node * p = _head;
	Node * q = p;
	while (p) {
		q = p->_link;
		delete p;
		p = q;
	}
}

void elloop_test::LinkList::print() const
{
	Node * node = _head;
	while (node) {
		p(node->_data);
		p(" ");
		node = node->_link;
	}
}

elloop_test::LinkList::Node::Node( int data /*= 0*/, Node * link /*= 0*/ ) : _data(data), _link(link), _id(++_counts)
{
	/*
	p("node ");
	p(mId);
	p(" created!");
	cr;
	*/
}

elloop_test::LinkList::Node::~Node()
{
	p("node ");
	p(_id);
	p(" deleted!");
	cr;
}

std::size_t elloop_test::LinkList::Node::_counts = 0;
